package com.atguigu.gulimall.product.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.vo.MemberResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class LoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberResponseVo> localVar = new ThreadLocal<>();
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        if(requestURI.contains("product/spuinfo")){
            return true;
        }
        String token = request.getParameter("token");
        if(token != null){
            String s = stringRedisTemplate.opsForValue().get("login:users:" + token);
            if(StringUtils.isEmpty(s)){
                MemberResponseVo memberResponseVo = JSON.parseObject(s , MemberResponseVo.class);
                localVar.set(memberResponseVo);
                return true;
            }
        }
        return false;
    }
}
